package com.example.demo.loader;

import com.sun.tools.javac.file.BaseFileObject;
import com.sun.tools.javac.file.JavacFileManager;

import java.io.*;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * 用来读取springboot项目中的class
 */
public class MemoryJavaClassObject extends BaseFileObject {

	private final String className;
	private URL url;

	MemoryJavaClassObject(String className, URL url, JavacFileManager javacFileManager) {
		super(javacFileManager);
		this.className = className;
		this.url = url;
	}

	@Override
	public Kind getKind() {
		return Kind.valueOf("CLASS");
	}

	@Override
	public URI toUri() {
		try {
			return url.toURI();
		} catch (URISyntaxException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public String getName() {
		return className;
	}

	@Override
	public InputStream openInputStream() {
		try {
			return url.openStream();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	@Override
	public OutputStream openOutputStream() throws IOException {
		return null;
	}

	@Override
	public CharSequence getCharContent(boolean ignoreEncodingErrors) throws IOException {
		return null;
	}

	@Override
	public Writer openWriter() throws IOException {
		return null;
	}

	@Override
	public long getLastModified() {
		return 0;
	}

	@Override
	public boolean delete() {
		return false;
	}

	public String inferBinaryName() {
		return className;
	}

	@Override
	public String getShortName() {
		return className.substring(className.lastIndexOf("."));
	}

	@Override
	protected String inferBinaryName(Iterable<? extends File> iterable) {
		return className;
	}

	@Override
	public boolean equals(Object o) {
		return false;
	}

	@Override
	public int hashCode() {
		return 0;
	}

	@Override
	public boolean isNameCompatible(String simpleName, Kind kind) {
		return false;
	}
}
